package com.free.space.controller.invoice;

import com.free.space.common.AmountToUpperTool;
import com.free.space.service.invoice.CalculateInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 发票计算，
 * 大写转换
 */
@Controller
@RequestMapping("invoiceController")
public class InvoiceController {
    @Autowired
    private CalculateInvoiceService calculateInvoiceService;
    @RequestMapping("invoiceView")
    public String invoiceView(){
        return "invoice/invoice.html";
    }

    /**
     * 计算发票数量
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("calculateInvoiceNum")
    @ResponseBody
    public String calculateInvoiceNum(HttpServletRequest request, HttpServletResponse response){
        String targetAmount = request.getParameter("targetAmount");
        String sourInvoice = request.getParameter("sourInvoice");
        String str = calculateInvoiceService.calculateInvoiceNum(targetAmount,sourInvoice);
        str = str + " 张数：" + (str.split(",").length-1);
        return str;
    }
    /**
     * 数字转大写数字
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("convertNumToUpper")
    @ResponseBody
    public String convertNumToUpper(HttpServletRequest request, HttpServletResponse response) {
        String num = request.getParameter("queryNum");
        BigDecimal bigDecimal = str2BigDecimal(num);
        return AmountToUpperTool.number2CNMontrayUnit(bigDecimal);
    }

    /**
     * 字符类型数字转数字类型
     *
     * @param str
     * @return
     */
    private BigDecimal str2BigDecimal(String str) {
        str = str.replaceAll(" ", "");
        return new BigDecimal(str);
    }
}
